package com.hx.zhg.util;



import com.hx.zhg.constant.CommonConstant;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {

    private static String DEFAULT_CIPHER_ALGORITHM="AES/CBC/PKCS5Padding";
    private static final String KEY_ALGORITHM = "AES";
    private static String sKey="6SD8UH6GR43DF7J9";
    private static String ivParameter="U87G6SH89K5FR3E2";

    // 加密
    public static String encrypt(String content) throws Exception {
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(content.getBytes(CommonConstant.CHARSET_UTF_8));
        return StringUtilExt.byte2hex(encrypted);//此处使用HEX。
    }

    // 解密
    public static String decrypt(String content) throws Exception {
        try {
            byte[] raw = sKey.getBytes(CommonConstant.CHARSET_UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] original = cipher.doFinal(StringUtilExt.hex2byte(content.getBytes(CommonConstant.CHARSET_UTF_8)));
            String originalString = new String(original,CommonConstant.CHARSET_UTF_8);
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

    // 加密
    public static String encrypt(byte[] key,byte[] iv,byte[] content) throws Exception {
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
        byte[] raw = key;
        SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
        IvParameterSpec ivs = new IvParameterSpec(iv);//使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivs);
        byte[] encrypted = cipher.doFinal(content);
        return StringUtilExt.byte2hex(encrypted);//此处使用HEX。
    }

    // 解密
    public static String decrypt(byte[] key,byte[] iv,byte[] content) throws Exception {
        try {
            byte[] raw = key;
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec ivs = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivs);
            byte[] original = cipher.doFinal(content);
            String originalString = new String(original,CommonConstant.CHARSET_UTF_8);
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        // 需要加密的字串
        String cSrc = "您好";
        System.out.println("加密前的字串是："+cSrc);
        // 加密
        String enString = AESUtil.encrypt(cSrc);
        System.out.println("加密后的字串是："+ enString);
        // 解密
        String DeString = AESUtil.decrypt("E0D0D731356D7795939F938D8ED3D7A4");
        System.out.println("解密后的字串是：" + DeString);
    }



}
